unit counterpartiesselect;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  Buttons, Grids;

type

  { TfrmCounterPartiesSelect }

  TfrmCounterPartiesSelect = class(TForm)
    btnSelect: TBitBtn;
    btnAddGroup: TBitBtn;
    btnCreate: TBitBtn;
    DBCounterParties: TDBGrid;
    procedure btnSelectClick(Sender: TObject);
    procedure btnAddGroupClick(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
    procedure DBCounterPartiesDblClick(Sender: TObject);
    procedure DBCounterPartiesDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure GetGroupTitle;
    procedure selectCounterParties;
  private
    { private declarations }
  public
    { public declarations }
    CurrentGroupRecordNumber:Integer;
  end;

var
  frmCounterPartiesSelect: TfrmCounterPartiesSelect;
  LastRecNo:String;
  groupTitle:String;
implementation

{$R *.lfm}

{ TfrmCounterPartiesSelect }

uses main,ZAbstractRODataset,counterpartiescreateedit,counterpartiescreateeditgroup;

procedure TfrmCounterPartiesSelect.GetGroupTitle;
begin
  groupTitle:='';
  if (CurrentGroupRecordNumber<>0)  then begin
     FrmMain.ZQuery.Active:=False;
     FrmMain.ZQuery.SQL.Text:='SELECT title FROM counterparties WHERE id='+IntToStr(CurrentGroupRecordNumber);
     FrmMain.ZQuery.Active:=True;
     groupTitle:=FrmMain.DSQuery.DataSet.FieldByName('title').AsString;
  end;
  frmCounterPartiesCreateEdit.edtGroup.Text:=groupTitle;
  FrmCounterPartiesCreateEditGroup.edtGroup.Text:=groupTitle;
  FrmMain.ZQuery.Active:=False;
end;

procedure TfrmCounterPartiesSelect.selectCounterParties;
begin
  if (DBCounterParties.DataSource.DataSet.FieldByName('group').AsBoolean=False) then begin
    FrmMain.counterPartieSelectedId:=DBCounterParties.DataSource.DataSet.FieldByName('id').AsInteger;
    FrmMain.counterPartieSelectedTitle:=DBCounterParties.DataSource.DataSet.FieldByName('title').AsString;
    Close;
  end;
end;

procedure TfrmCounterPartiesSelect.btnCreateClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSCounterParties.DataSet.Append;
  frmCounterPartiesCreateEdit.CurrentGroupRecordNumber:=CurrentGroupRecordNumber;
  frmCounterPartiesCreateEdit.ShowModal;
end;

procedure TfrmCounterPartiesSelect.DBCounterPartiesDblClick(Sender: TObject);
var
  id,parentid:String;
  group:Boolean;
begin
  id:=DBCounterParties.DataSource.DataSet.FieldByName('id').AsString;
  parentid:=DBCounterParties.DataSource.DataSet.FieldByName('parentid').AsString;
  group:=DBCounterParties.DataSource.DataSet.FieldByName('group').AsBoolean;
  if (group)  AND (LastRecNo=id) then begin
    FrmMain.ZTCounterParties.Filtered:=false;
    FrmMain.ZTCounterParties.SortedFields:='group,id,title';
    FrmMain.ZTCounterParties.SortType:=stAscending;
    FrmMain.ZTCounterParties.Filter:='id='+parentid+' or parentid='+parentid;
    FrmMain.ZTCounterParties.Filtered:=true;
    LastRecNo:=parentid;
    CurrentGroupRecordNumber:=StrToInt(parentid);
  end else begin
  if ((group) AND (parentid<>id))
  then begin
    FrmMain.ZTCounterParties.Filtered:=false;
    FrmMain.ZTCounterParties.SortedFields:='group,id,title';
    FrmMain.ZTCounterParties.SortType:=stAscending;
    FrmMain.ZTCounterParties.Filter:='id='+id+' or parentid='+id;
    FrmMain.ZTCounterParties.Filtered:=true;
    LastRecNo:=id;
    CurrentGroupRecordNumber:=StrToInt(id);
  end;
  end;
  if (NOT group) then begin
    selectCounterParties;
  end;
end;

procedure TfrmCounterPartiesSelect.DBCounterPartiesDrawColumnCell(
  Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
bmp: TBitmap;
begin
    if (Column.FieldName = 'id') and (DBCounterParties.DataSource.DataSet.FieldByName('group').AsBoolean) then begin
       bmp := TBitmap.Create;
       if (Column.Field.AsString=LastRecNo) then begin
         FrmMain.ImageList2.GetBitmap(3,bmp);
       end else begin
         FrmMain.ImageList2.GetBitmap(0,bmp);
       end;
       DBCounterParties.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end else if (Column.FieldName = 'id') then begin
       bmp := TBitmap.Create;
       FrmMain.ImageList2.GetBitmap(2,bmp);
       DBCounterParties.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end;
end;

procedure TfrmCounterPartiesSelect.FormCreate(Sender: TObject);
begin
  FrmMain.ZTCounterParties.Filtered:=false;
  FrmMain.ZTCounterParties.SortedFields:='group,id,title';
  FrmMain.ZTCounterParties.SortType:=stAscending;
  FrmMain.ZTCounterParties.Filter:='parentid=0';
  FrmMain.ZTCounterParties.Filtered:=true;
  CurrentGroupRecordNumber:=0;
end;

procedure TfrmCounterPartiesSelect.FormShow(Sender: TObject);
begin
  FrmMain.counterPartieSelectedId:=0;
  FrmMain.counterPartieSelectedTitle:=EmptyStr;
  FrmMain.DSCounterParties.DataSet.Refresh;
end;

procedure TfrmCounterPartiesSelect.btnAddGroupClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSCounterPartiesGroup.DataSet.Append;
  frmCounterPartiesCreateEditGroup.ShowModal;
end;

procedure TfrmCounterPartiesSelect.btnSelectClick(Sender: TObject);
begin
  selectCounterParties;
end;

end.

